package com.bdzl.admin.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.lang.generator.ObjectGenerator;
import com.bdzl.admin.service.sys.OrgService;
import com.bdzl.pub.common.Tree;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.sys.OrgDO;
import com.bdzl.pub.util.param.BuildTree;
import com.bdzl.pub.vo.sys.OrgDeptVO;
import io.netty.util.Constant;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.bdzl.pub.entity.sys.OrgDeptDO;
import com.bdzl.admin.service.sys.OrgDeptService;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;

/**
 * 部门信息表
 * 
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-03-23 13:59:22
 */
 
@Controller
@RequestMapping("/bdzl/orgDept")
public class OrgDeptController extends BaseController {
	@Autowired
	private OrgDeptService orgDeptService;
	@Autowired
	private OrgService orgService;
	
	@GetMapping()
	String OrgDept(){
	    return "bdzl/orgDept/orgDept";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<OrgDeptVO> orgDeptList = orgDeptService.list(query);
		int total = orgDeptService.count(query);
		PageUtils pageUtils = new PageUtils(orgDeptList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "bdzl/orgDept/add";
	}

	/**
	 * 编辑回显
	 * @param map   deptNo 部门ID
	 * @return
	 */
	@ResponseBody
	@PostMapping("/edit")
	R edit(@RequestBody Map<String,Object> map){
		//deptNo 部门ID
		OrgDeptDO orgDept = orgDeptService.get(map.get("deptNo").toString());
		OrgDeptVO orgDeptVO = new OrgDeptVO();
		//类拷贝
		BeanUtils.copyProperties(orgDept, orgDeptVO);
		if (!orgDeptVO.getDeptPno().equals("-1")&&!orgDeptVO.getDeptPno().equals("1")){
			//查询上级部门
			OrgDeptDO orgDeptDO = orgDeptService.get(orgDeptVO.getDeptPno());
			orgDeptVO.setDeptPname(orgDeptDO.getDeptName());
		}else {
			//-1为机构
			orgDeptVO.setDeptPname(orgService.get("1").getOrgName());
		}
		return R.ok(orgDeptVO);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(@RequestBody OrgDeptDO orgDept){
		int save = orgDeptService.save(orgDept);
		if(save>0){
			return R.ok();
		}else if (save == -10){
			return R.error("部门编号重复！");
		} else if (save == -11){
			return R.error("部门名称重复！");
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(@RequestBody OrgDeptDO orgDept){
		orgDeptService.update(orgDept);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	public R remove(@RequestBody Map<String,Object> map){
		//deptNo 部门ID
		int remove = orgDeptService.remove(map.get("deptNo").toString());
		if(remove>0){
			if (remove==99){
				return R.error("该部门下有员工不能删除！");
			}
			if (remove==100){
				return R.error("该部门有下级部门不能删除！");
			}
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") String[] deptNos){
		int i = orgDeptService.batchRemove(deptNos);
		if(i>0){
			if (i==99){
				return R.error("部门下有员工不能删除！");
			}
			if (i==100){
				return R.error("部门有下级部门不能删除！");
			}
		}
		return R.ok();
	}

	/**
	 * 部门树
	 * @return
	 */
	@PostMapping( "/Tree")
	@ResponseBody
	public Tree<OrgDeptDO> getTree() {
		List<Tree<OrgDeptDO>> trees = new ArrayList<>();
		OrgDO orgDO = orgService.get("1");
		//管理员角色
		Tree<OrgDeptDO> tree = new Tree<>();
		tree.setId("1");
		tree.setParentId("0");
		tree.setText(orgDO.getOrgName());
		Map<String, Object> state = new HashMap<>(16);
		state.put("opened", true);
		tree.setState(state);
		trees.add(tree);
		Map<String, Object> map = new HashMap<>();
		map.put("deptOrg",orgDO.getOrgId());
		List<OrgDeptVO> orgDeptDOs= orgDeptService.list(map);
		for (OrgDeptDO orgDeptDO : orgDeptDOs) {
			Tree<OrgDeptDO> deptDOTree = new Tree<>();
			deptDOTree.setId(String.valueOf(orgDeptDO.getDeptNo()));
			deptDOTree.setParentId(String.valueOf(orgDeptDO.getDeptPno()));
			deptDOTree.setText(orgDeptDO.getDeptName());
			state.put("opened", true);
			deptDOTree.setState(state);
			trees.add(deptDOTree);
		}
		Tree<OrgDeptDO> build = BuildTree.build(trees);
		return build;
	}
}
